$(function(){
	//点击刷新验证码
	$("#imgid").click(function(){
		//模板分配过来的变量
	    VerifyUrl=$(this).attr('src').replace('.html','');
		$(this).attr('src',VerifyUrl+'/'+Math.random());
	});

	var mask=true;
	$(".J_updateAccount").click(function(){
		if(mask){
			$(this).html('收起');
			$(this).parent().parent().parent().addClass('open');

			if(bindEmail==''){
				$('.step2').show();
			}else{
				$('.step6').show();
			}
			mask=false;
		}else{
			$(this).html('设置');
			$(this).parent().parent().parent().removeClass('open');
			mask=true;
		}
	});

	var checkEmail;//判断邮箱是否输入正确
	//邮箱认证
	$('.J_email_confirm').click(function(){
		if(checkEmail){
			var email=$('#J_email_text').val();
			$.ajax({
				type : "post",
				data : {"email":email},
				url : editEmail,
				dataType : "json",
				success : function(d){
					if(d=='ok'){
						$("#J_old_email_txt").html(email);
						$('.step2').hide();
						$('.step3').show();
					}else{
						$('.step2').hide();
						$('.step5').show();
					}
				}
			});
			
		}

	});

	//更改邮箱
	$(".J_change_email").click(function(){
		$.ajax({
			type : "post",
			data : {"email":bindEmail,'edit':'1'},
			url : editEmail,
			dataType : "json",
			success : function(d){
				if(d=='ok'){
					$("#J_new_email_txt").html(bindEmail);
					$('.step6').hide();
					$('.step7').show();
				}else{
					$('.step2').hide();
					$('.step5').show();
				}
			}
		});
	});

	//邮箱格式验证
	$('#J_email_text').blur(function(){
		var email=$(this).val();
		var reg=/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
		if(!email.match(reg)){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).next().next().html('请输入正确的邮箱地址').css('color','red').show();
			$(this).css('border','1px solid red');
			checkEmail=false;
		}else{

			if(bindEmail==email){
				$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
				$(this).next().next().html('请不要更改为与原邮箱一致').css('color','red').show();
				$(this).css('border','1px solid red');
				checkEmail=false;
				return;
			}
			//异步验证邮箱是否存在
			$.ajax({
				type : "post",
				data : {"email":email},
				url : checkEmailTrue,
				dataType : "json",
				success : function(d){
					if(d=='ok'){
						$("#J_email_text").next().attr('src',pub+'/Images/checked.gif').show();
						$("#J_email_text").next().next().html('');
						$("#J_email_text").css('border','1px solid lightblue');
						checkEmail=true;
					}else{
						$("#J_email_text").next().attr('src',pub+'/Images/unchecked.gif').show();
						$("#J_email_text").next().next().html('邮箱已存在').css('color','red').show();
						$("#J_email_text").css('border','1px solid red');
						checkEmail=false;
					}
				}
			});
			
		}

	});

	//重新发送邮件
	$(".J_new_again_send_email").click(function(){
		$.ajax({
			type : "post",
			data : {"email":bindEmail,'edit':'1'},
			url : editEmail,
			dataType : "json",
			success : function(d){
				if(d=='ok'){
					$("#J_new_email_txt").html(bindEmail);
					$('.step6').hide();
					$('.step7').show();
				}else{
					$('.step2').hide();
					$('.step5').show();
				}
			}
		});
	});

	//重新发送邮件
	$(".J_old_again_send_email").click(function(){
		var email=$('#J_email_text').val();
		$.ajax({
			type : "post",
			data : {"email":email},
			url : editEmail,
			dataType : "json",
			success : function(d){
				if(d=='ok'){
					$("#J_old_email_txt").html(bindEmail);
					$('.step6').hide();
					$('.step3').show();
				}else{
					$('.step2').hide();
					$('.step5').show();
				}
			}
		});
	});

	//更换手机号码
	$('#changeNum').click(function(){
		$('.phone_step2').hide();
		$('.phone_step1').show();
	});
	var	checkPhone;
	//修改手机操作
	$('#mobile_auth_step2_submit').click(function(){
		if(checkPhone){
			var phone=$('#J_phone_text').val();
			$.ajax({
				type : "post",
				data : {"phone":phone},
				url : editPhone,
				dataType : "json",
				success : function(d){
					if(d=='ok'){
						$("#J_mobile_authentication_no").html(phone);
						$('#telphones').html(phone);
						$('.phone_step1').hide();
						$('.phone_step2').show();
					}else{
						$('.phone_step1').hide();
						$('.phone_step3').show();
					}
				}
			});
			
		}

	});

	//验证手机号码
	$('#J_phone_text').blur(function(){
		var phone=$(this).val();
		var reg=/^[1][0-9]{10}$/;
		if(!phone.match(reg)){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).next().next().html('请输入正确的手机号码').css('color','red').show();
			$(this).css('border','1px solid red');
			checkPhone=false;
		}else{

			if(bindPhone==phone){
				$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
				$(this).next().next().html('请不要更改为与原手机号码一致').css('color','red').show();
				$(this).css('border','1px solid red');
				checkPhone=false;
				return;
			}
			//异步验证邮箱是否存在
			$.ajax({
				type : "post",
				data : {"tel":phone},
				url : checkPhoneTrue,
				dataType : "json",
				success : function(d){
					if(d=='ok'){
						$("#J_phone_text").next().attr('src',pub+'/Images/checked.gif').show();
						$("#J_phone_text").next().next().html('');
						$("#J_phone_text").css('border','1px solid lightblue');
						checkPhone=true;
					}else{
						$("#J_phone_text").next().attr('src',pub+'/Images/unchecked.gif').show();
						$("#J_phone_text").next().next().html('手机号码已存在').css('color','red').show();
						$("#J_phone_text").css('border','1px solid red');
						checkPhone=false;
					}
				}
			});
			
		}
	});

	//验证密码和确认密码
	$("input[name=pwd]").blur(function(){
		if(!$(this).val().match(/^[a-zA-Z][\w]{4,16}$/)){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).next().next().html('密码由字母开头，数字 _ 组成5-17位组成').css('color','red').show();
			$(this).css('border','1px solid red');
			num2=1;
		}else{
			$(this).next().attr('src',pub+'/Images/checked.gif').show();
			$(this).next().next().html('');
			$(this).css('border','1px solid lightblue');
			num2=2;
		}
	});

	$("input[name=pwded]").blur(function(){
		var pass=$(this).val();
		if(pass!=$("input[name=pwd]").val() || !pass){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).next().next().html('两次密码不一致').css('color','red').show();
			$(this).css('border','1px solid red');
			num3=1;
		}else{
			$(this).next().attr('src',pub+'/Images/checked.gif').show();
			$(this).next().next().html('');
			$(this).css('border','1px solid lightblue');
			num3=3;
		}
	});

	//验证码
	$("input[name=verify]").blur(function(){
		var verify = $(this).val();
		$.ajax({
			"type" : "post",
			"data" : {"verify":verify},
			"url" : checkVerify,
			"dataType" : "json",
			"success" : function(d){
				if(d=='ok'){
					$("input[name=verify]").next().attr('src',pub+'/Images/checked.gif').show();
					$("input[name=verify]").css('border','1px solid lightblue');
					checkPhone=true;
					num1=1
				}else{
					$("input[name=verify]").next().attr('src',pub+'/Images/unchecked.gif').show();
					$("input[name=verify]").css('border','1px solid red');
					num1=0
					checkPhone=false;
				}
			}
		});
	});

	//原密码不能为空
	$("input[name=oldpwd]").blur(function(){
		var pass=$(this).val();
		if(!pass){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).next().next().html('原密码不能为空').css('color','red').show();
			$(this).css('border','1px solid red');
			num4=1;
		}else{
			$(this).next().attr('src',pub+'/Images/checked.gif').show();
			$(this).next().next().html('');
			$(this).css('border','1px solid lightblue');
			num4=4;
		}
	});

	//提交事件
	$("input[name=sub]").click(function(){
		if(($("input[name=oldpwd]").val() == "") || ($("input[name=verify]").val() == "") || ($("input[name=pwd]").val() == "") || ($("input[name=pwded]").val() == "") || num1+num2+num3+num4!=10){
				return false;
			}
			
	});
});